import { NextFunction, Request, Response } from "express";
import { AppError } from "../utils";

export const requireLogin = (
  req: Request,
  res: Response,
  next: NextFunction,
) => {
  try {
    const user = res.locals.user;
    if (!user) {
      return next(
        new AppError(401, `Session has expired or user doesn't exist`),
      );
    }
    next();
  } catch (err) {
    next(err);
  }
};
